//
//  PrivateTaxCalcMasterViewController.m
//  PrivateTaxCalculator
//
//  Created by albert ma on 12/21/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "PrivateTaxCalcMasterViewController.h"

#import "PrivateTaxCalcDetailViewController.h"

#import "SQLiteTaxDao.h"

@implementation PrivateTaxCalcMasterViewController
@synthesize incomeTextField;
@synthesize insurenceTextField;
@synthesize taxTextField;
@synthesize needPayIncomeTextFieldOld;
@synthesize needPayIncomeTextFieldNew;
@synthesize postTaxIncomeTextFieldOld;
@synthesize postTaxIncomeTextFieldNew;


@synthesize managedObjectContext, fetchedResultsController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
    }
   
    return self;
}
							
- (void)dealloc
{
    [incomeTextField release];
    [insurenceTextField release];
    [taxTextField release];
    [needPayIncomeTextFieldOld release];
    [needPayIncomeTextFieldNew release];
    [postTaxIncomeTextFieldOld release];
    [postTaxIncomeTextFieldNew release];
    [privateTaxCalulator release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    // Set up the edit and add buttons.
    privateTaxCalulator = [[PrivateTaxCalculator alloc] init];
    [privateTaxCalulator setTaxDao:[SQLiteTaxDao instance]];
    self.navigationController.navigationBarHidden = YES;
    UIImage* paper = [UIImage imageNamed:@"paper_material"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:paper];
    
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)] autorelease];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)viewDidUnload
{
    [self setIncomeTextField:nil];
    [self setInsurenceTextField:nil];
    [self setTaxTextField:nil];
    [self setNeedPayIncomeTextFieldOld:nil];
    [self setNeedPayIncomeTextFieldNew:nil];
    [self setPostTaxIncomeTextFieldOld:nil];
    [self setPostTaxIncomeTextFieldNew:nil];
    self.fetchedResultsController = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void) disappearSoftKeyboard
{
    [incomeTextField resignFirstResponder];
    [insurenceTextField resignFirstResponder];
}

- (IBAction)touchScreen:(id)sender 
{
    [self disappearSoftKeyboard];

}

- (IBAction)clearUpAllInput:(id)sender {
    [incomeTextField setText:@""];
    [insurenceTextField setText:@""];
    [taxTextField setText:@""];
    [needPayIncomeTextFieldOld setText:@""];
    [needPayIncomeTextFieldNew setText:@""];
    [postTaxIncomeTextFieldOld setText:@""];
    [postTaxIncomeTextFieldNew setText:@""];
    [self disappearSoftKeyboard];
}

- (IBAction)goToDetailView:(id)sender 
{
    PrivateTaxCalcDetailViewController * f = [[[PrivateTaxCalcDetailViewController alloc]initWithNibName:@"PrivateTaxCalcDetailViewController" bundle:nil]autorelease];
    [f setPrivateTaxCalculator:privateTaxCalulator];
     NSString *incomeString = incomeTextField.text;
    double rawIncome = [incomeString doubleValue];
    double insurance =[insurenceTextField.text doubleValue];
    if(rawIncome > 0 && insurance > 0)
    {
        if(rawIncome < insurance)
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"收入小于三险一金" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [alertView release];
        }
        else
        {
            [f setTotalInsurance:[NSNumber numberWithDouble:insurance]];
            [f setRawIncome:[NSNumber numberWithDouble:rawIncome]];
            [self.navigationController pushViewController:f animated:YES];
        }
    }
    else
    {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收入或三险一金不能为空" message:@"请输入数字后，点击详细信息按钮" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
    }
   
    
}

- (IBAction)calculatePrivateTax:(id)sender
{
    NSString *incomeString = incomeTextField.text;
    NSString *insuranceString = insurenceTextField.text;
    if(incomeString.length > 0 && insuranceString.length > 0)
    {
        double incomeAmount = [incomeString doubleValue];
        double insuranceAmount = [insuranceString doubleValue];
        if (incomeAmount < insuranceAmount) {
             UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"收入小于三险一金" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [alertView release];
            return;
        }
        double newTax = [privateTaxCalulator getTax:incomeAmount insurance:insuranceAmount withType:TAX_2011];
        double oldTax = [privateTaxCalulator getTax:incomeAmount insurance:insuranceAmount withType:TAX_2009];
        double needPayTaxIncomeOld = [privateTaxCalulator getTaxIncome:incomeAmount insurance:insuranceAmount withType:TAX_2009];
        double needPayTaxIncomeNew = [privateTaxCalulator getTaxIncome:incomeAmount insurance:insuranceAmount withType:TAX_2011];
        double postTaxIncomeOld = incomeAmount - insuranceAmount - oldTax;
        double postTaxIncomeNew = incomeAmount - insuranceAmount - newTax;
        
        
        [taxTextField setText:[NSString stringWithFormat:@"%.2f", newTax]];
        [needPayIncomeTextFieldOld setText:[NSString stringWithFormat:@"%.2f", needPayTaxIncomeOld]];
        [needPayIncomeTextFieldNew setText:[NSString stringWithFormat:@"%.2f", needPayTaxIncomeNew]];
        [postTaxIncomeTextFieldOld setText:[NSString stringWithFormat:@"%.2f", postTaxIncomeOld]];
        [postTaxIncomeTextFieldNew setText:[NSString stringWithFormat:@"%.2f", postTaxIncomeNew]];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收入或三险一金不能为空" message:@"请输入数字后，点击计算" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];
        [alertView release];
       
    }
    [self disappearSoftKeyboard];
}
@end
